const app = getApp();
const pageSize = 20;
const statusList = [{
  value: "",
  name: '全部'
}, {
  value: 1,
  name: '正常'
}, {
  value: 2,
  name: '禁用'
}]
Page({
  data: {
    delete_icon: '/images/del.png',
    edit_icon: '/images/edit.png',
    searchImg: '/images/search.png',
    page: 1, //当前页数
    promotionList: [],
    drawer: false,
    isFilter: false,
    conditions: {
      start_time: '', //开始时间
      end_time: '', //结束时间‘
      statusIndex: "", //搜索条件 状态
      promotion_name: '', //搜索条件 项目名称
    },
    loadmore: false,
    nomore: false,
    statusList,
    empty: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getPromotionList();
  },



  onPullDownRefresh: function () {
    this.reloadData();
  },

  onReachBottom: function () {
    if (this.data.loadmore) {
      this.setData({
        page: this.data.page + 1
      })
      this.getPromotionList();
    }
  },


  reloadData() {
    this.setData({
      loadmore: false,
      nomore: false,
      page: 1,
      promotionList: [],
    })
    wx.showLoading();
    this.getPromotionList();
  },


  //获取促销项目列表
  getPromotionList: function () {
    app.ajax({
      url: 'Partner/Promotion/getPromotionList',
      data: {
        row: pageSize,
        page: this.data.page,
        promotion_name: this.data.conditions.promotion_name,
        status: this.data.conditions.statusIndex !== "" ? this.data.statusList[this.data.conditions.statusIndex].value : "",
        start_time: this.data.conditions.start_time,
        end_time: this.data.conditions.end_time,
      },
      success: (res) => {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        if (res.data.code == 1000) {
          const source = res.data.data;
          const list = this.data.promotionList.concat(source.data);
          this.setData({
            promotionList: list,
            drawer: false,
            empty: source.page.total_number == 0 ? true : false,
            nomore: list.length >= source.page.total_number ? true : false,
            loadmore: list.length < source.page.total_number ? true : false,
          })
        } else {
          app.showToast(res.data.message);
        }
      },
      error: e => {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        app.showToast("网络错误");
      }
    })
  },

  //编辑
  edit(e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../projectAdd/projectAdd?type=1&id=' + id,
    })
  },

  //删除
  delete(e) {
    var that = this;
    wx.showModal({
      content: '是否删除此项目?',
      confirmText: '确认',
      cancelText: '取消',
      success: function (res) {
        if (res.confirm) {
          wx.hideLoading();
          var id = e.currentTarget.dataset.id;
          app.ajax({
            url: 'Partner/Promotion/deletePromotion',
            data: {
              id: id,
            },
            success: function (res) {
              wx.hideLoading();
              if (res.data.code == 1000) {
                app.showToast(res.data.message, "success");
                setTimeout(() => {
                  that.reloadData();
                }, 1500)
              } else {
                app.showToast(res.data.message)
              }
            },
            error: e => {
              wx.hideLoading();
            }
          })
        }
      },
    })
  },
  //添加
  add() {
    wx.navigateTo({
      url: '../projectAdd/projectAdd?type=2',
    })
  },


  handleTime(e) {
    const name = e.currentTarget.dataset.name;
    this.setData({
      [`conditions.${name}`]: e.detail.value
    })
  },


  handleChange(e) {
    const {
      index,
      name
    } = e.currentTarget.dataset;
    this.setData({
      [`conditions.${name}`]: index
    })
  },



  showDrawer() {
    this.condition = {
      ...this.data.conditions
    };
    this.setData({
      drawer: true,
    })
  },

  closeDrawer() {
    this.setData({
      drawer: false,
      conditions: this.condition
    })
    delete(this.condition)
  },

  resetDrawer() {
    const conditions = this.data.conditions;
    for (let x in conditions) {
      conditions[x] = "";
    }
    this.setData({
      conditions,
    })
  },

  confirmDrawer() {
    let isFilter = false;
    const conditions = this.data.conditions;
    for (let x in conditions) {
      conditions[x] !== "" && (isFilter = true);
    }
    this.setData({
      isFilter
    })
    delete(this.condition);
    this.reloadData();
  },

})